home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 5.4 KB | 207 lines | [TEXT/MPS ] |
- // The C++ Booch Components (Version 2.1)
- // (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
- //
- // Restricted Rights Legend
- // Use, duplication, or disclosure is subject to restrictions as bag forth
- // in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer
- // Software clause at DFARS 252.227-7013.
- //
- // BCMap.cpp
- //
- // This file contains the definitions for the map abstract base class
- // and its iterators.
-
- #include "BCMap.h"
-
- template<class Item, class Value>
- BC_TMap<Item, Value>::BC_TMap() {}
-
- template<class Item, class Value>
- BC_TMap<Item, Value>::BC_TMap(const BC_TMap<Item, Value>&) {}
-
- template<class Item, class Value>
- BC_TMap<Item, Value>::~BC_TMap() {}
-
- template<class Item, class Value>
- BC_TMap<Item, Value>& BC_TMap<Item, Value>::operator=(const BC_TMap<Item, Value>& m)
- {
- if (this == &m)
- return *this;
- ((BC_TMap<Item, Value>&)m).Lock();
- Purge();
- BC_TMapActiveIterator<Item, Value> iter(m);
- while (!iter.IsDone()) {
- Attach(*iter.CurrentItem(), *iter.CurrentValue());
- iter.Next();
- }
- ((BC_TMap<Item, Value>&)m).Unlock();
- return *this;
- }
-
- template<class Item, class Value>
- BC_Boolean BC_TMap<Item, Value>::operator==(const BC_TMap<Item, Value>& m) const
- {
- if (this == &m)
- return 1;
- ((BC_TMap<Item, Value>&)m).Lock();
- if (Cardinality() != m.Cardinality()) {
- ((BC_TMap<Item, Value>&)m).Unlock();
- return 0;
- }
- BC_TMapActiveIterator<Item, Value> iter(*this);
- while (!iter.IsDone()) {
- if (!m.Exists(*iter.CurrentItem())) {
- ((BC_TMap<Item, Value>&)m).Unlock();
- return 0;
- }
- iter.Next();
- }
- ((BC_TMap<Item, Value>&)m).Unlock();
- return 1;
- }
-
- template<class Item, class Value>
- BC_Boolean BC_TMap<Item, Value>::operator!=(const BC_TMap<Item, Value>& m) const
- {
- return !operator==(m);
- }
-
- template<class Item, class Value>
- void BC_TMap<Item, Value>::Lock() {}
-
- template<class Item, class Value>
- void BC_TMap<Item, Value>::Unlock() {}
-
- template<class Item, class Value>
- BC_TMapActiveIterator<Item, Value>::
- BC_TMapActiveIterator(const BC_TMap<Item, Value>& m)
- : fMap(m),
- fBucketIndex(m.Cardinality() ? 0 : -1),
- fIndex(-1)
- {
- if (fBucketIndex == 0) {
- for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- return;
- }
- }
- }
-
- template<class Item, class Value>
- BC_TMapActiveIterator<Item, Value>::~BC_TMapActiveIterator() {}
-
- template<class Item, class Value>
- void BC_TMapActiveIterator<Item, Value>::Reset()
- {
- fBucketIndex = fMap.Cardinality() ? 0 : -1;
- fIndex = -1;
- if (fBucketIndex == 0) {
- for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- return;
- }
- }
- }
-
- template<class Item, class Value>
- BC_Boolean BC_TMapActiveIterator<Item, Value>::Next()
- {
- if (fBucketIndex < fMap.NumberOfBuckets()) {
- fIndex++;
- if (fIndex >= fMap.Length(fBucketIndex)) {
- fBucketIndex++;
- fIndex = - 1;
- for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- break;
- }
- }
- return !IsDone();
- } else
- return 0;
- }
-
- template<class Item, class Value>
- BC_Boolean BC_TMapActiveIterator<Item, Value>::IsDone() const
- {
- if ((fBucketIndex < 0) || (fBucketIndex >= fMap.NumberOfBuckets()))
- return 1;
- else {
- if (fIndex >= fMap.Length(fBucketIndex)) {
- ((BC_TMapActiveIterator<Item, Value>&)(*this)).fBucketIndex++;
- ((BC_TMapActiveIterator<Item, Value>&)(*this)).fIndex = -1;
- for (; (fBucketIndex < fMap.NumberOfBuckets());
- ((BC_TMapActiveIterator<Item, Value>&)(*this)).fBucketIndex++) {
- ((BC_TMapActiveIterator<Item, Value>&)(*this)).fIndex =
- fMap.Length(fBucketIndex) ? 0 : - 1;
- if (fIndex != -1)
- return 0;
- }
- return 1;
- }
- return 0;
- }
- }
-
- template<class Item, class Value>
- const Item* BC_TMapActiveIterator<Item, Value>::CurrentItem() const
- {
- return IsDone() ? 0 : &fMap.ItemAt(fBucketIndex, fIndex);
- }
-
- template<class Item, class Value>
- Item* BC_TMapActiveIterator<Item, Value>::CurrentItem()
- {
- return IsDone() ? 0 : &((Item&)(fMap.ItemAt(fBucketIndex, fIndex)));
- }
-
- template<class Item, class Value>
- const Value* BC_TMapActiveIterator<Item, Value>::CurrentValue() const
- {
- return IsDone() ? 0 : &fMap.ValueAt(fBucketIndex, fIndex);
- }
-
- template<class Item, class Value>
- Value* BC_TMapActiveIterator<Item, Value>::CurrentValue()
- {
- return IsDone() ? 0 : &((Value&)(fMap.ValueAt(fBucketIndex, fIndex)));
- }
-
- template<class Item, class Value>
- BC_TMapPassiveIterator<Item, Value>::
- BC_TMapPassiveIterator(const BC_TMap<Item, Value>& m)
- : fMap(m) {}
-
- template<class Item, class Value>
- BC_TMapPassiveIterator<Item, Value>::~BC_TMapPassiveIterator() {}
-
- template<class Item, class Value>
- BC_Boolean BC_TMapPassiveIterator<Item, Value>::
- Apply(BC_Boolean (*f)(const Item&, const Value&))
- {
- BC_TMapActiveIterator<Item, Value> iter(fMap);
- while (!iter.IsDone()) {
- if (!f(*iter.CurrentItem(), *iter.CurrentValue()))
- return 0;
- else
- iter.Next();
- }
- return 1;
- }
-
- template<class Item, class Value>
- BC_Boolean BC_TMapPassiveIterator<Item, Value>::Apply(BC_Boolean (*f)(Item&, Value&))
- {
- BC_TMapActiveIterator<Item, Value> iter(fMap);
- while (!iter.IsDone()) {
- if (!f(*iter.CurrentItem(), *iter.CurrentValue()))
- return 0;
- else
- iter.Next();
- }
- return 1;
- }
-